package com.fsj.miniapp.service.impl;

import com.fsj.miniapp.entity.Goods;
import com.fsj.miniapp.entity.GoodsBasicInfo;
import com.fsj.miniapp.service.GoodsBasicInfoService;
import com.fsj.miniapp.service.GoodsPicService;
import com.fsj.miniapp.service.GoodsService;
import com.fsj.miniapp.vo.GoodsBasicInfoVo;
import com.fsj.miniapp.vo.GoodsDetailVo;
import com.fsj.miniapp.vo.GoodsPicVo;
import com.fsj.miniapp.vo.GoodsVo;
import com.google.common.collect.Lists;
import com.stip.mybatis.generator.plugin.BaseExample;
import com.stip.mybatis.generator.plugin.BaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class GoodsServiceImpl extends BaseService<Goods, BaseExample, Long> implements GoodsService {

    @Autowired
    private GoodsBasicInfoService goodsBasicInfoService;

    @Autowired
    private GoodsPicService goodsPicService;


    @Override
    public List<GoodsVo> list(Long merchantId) {
        BaseExample example = new BaseExample();
        example.createCriteria().addCriterion("merchant_id=", merchantId);
        example.setOrderByClause(" sorted asc");
        List<Goods> goods = selectByExample(example);

        if (!CollectionUtils.isEmpty(goods)) {
            return goods.stream().map(f -> {
                GoodsVo res = new GoodsVo();
                BeanUtils.copyProperties(f, res);
                res.setMinPrice(f.getGoodsPrice());
                res.setName(f.getGoodsName());
                return res;
            }).collect(Collectors.toList());
        }

        return Lists.newArrayList();
    }

    @Override
    public GoodsDetailVo getDetail(Long goodsId) {

        GoodsDetailVo detailVo = new GoodsDetailVo();

        GoodsBasicInfoVo basicInfoVo = goodsBasicInfoService.getById(goodsId);
        detailVo.setBasicInfo(basicInfoVo);
        detailVo.setContent(basicInfoVo.getContent());
        List<GoodsPicVo> goodsPicVos = goodsPicService.getById(goodsId);
        detailVo.setPics(goodsPicVos);
        return detailVo;
    }
}